//
//  TTConfig.m
//  tiantu_offer
//
//  Created by zwb on 16/4/26.
//  Copyright © 2016年 zwb. All rights reserved.
//

#import "TTConfig.h"
#import "CommonCrypto/CommonDigest.h"
// JPush
//#import "JPUSHService.h"
@implementation TTConfig

+ (id)requestParamenters:(id)parament
{
    NSMutableDictionary *parameterDict = nil;
    if (parament) {
        parameterDict = [[NSMutableDictionary alloc] initWithDictionary:parament];
    } else {
        parameterDict = [[NSMutableDictionary alloc] init];
    }
    
    if (parameterDict && [parameterDict[ISNOTADDTOKENID] boolValue]) {
//        if ([ProgectCatch islogin] && [ProgectCatch getuser_token]) {
//            [parameterDict setObject:[ProgectCatch getuser_token] forKey:@"token"];
////            [parameterDict setObject:[ProgectCatch getuser_id] forKey:@"user_id"];
//        }
    }
    
    if (parameterDict[ISNOTADDTOKENID]) {
        [parameterDict removeObjectForKey:ISNOTADDTOKENID];
    }
    
//    [parameterDict setObject:[ProgectMemory GetDeviceLocalCountry] forKey:@"country"];
    //[parameterDict setObject:@"1" forKey:@"user_redis"];
    
    //[parameterDict setObject:@"sg" forKey:@"country"];
    //NSLog(@"parameterDict: %@", parameterDict);
    
    return parameterDict;
}
+(BOOL)isPureNum:(NSString *)text {
    if (!text) {
        return NO;
    }
    NSScanner *scan = [NSScanner scannerWithString:text];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}


+ (UIImage *)rotateImage:(UIImage *)aImage
{
    CGImageRef imgRef = aImage.CGImage;
    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);
    CGAffineTransform transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, width, height);
    CGFloat scaleRatio = 1;
    CGFloat boundHeight;
    UIImageOrientation orient = aImage.imageOrientation;
    switch(orient)
    {
        case UIImageOrientationUp: //EXIF = 1
            transform = CGAffineTransformIdentity;
            break;
        case UIImageOrientationUpMirrored: //EXIF = 2
            transform = CGAffineTransformMakeTranslation(width, 0.0);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            break;
        case UIImageOrientationDown: //EXIF = 3
            transform = CGAffineTransformMakeTranslation(width, height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;
        case UIImageOrientationDownMirrored: //EXIF = 4
            transform = CGAffineTransformMakeTranslation(0.0, height);
            transform = CGAffineTransformScale(transform, 1.0, -1.0);
            break;
        case UIImageOrientationLeftMirrored: //EXIF = 5
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(height, width);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;
        case UIImageOrientationLeft: //EXIF = 6
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(0.0, width);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;
        case UIImageOrientationRightMirrored: //EXIF = 7
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeScale(-1.0, 1.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;
        case UIImageOrientationRight: //EXIF = 8
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(height, 0.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;
        default:
            break;
    }
    UIGraphicsBeginImageContext(bounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
        CGContextScaleCTM(context, -scaleRatio, scaleRatio);
        CGContextTranslateCTM(context, -height, 0);
    }
    else {
        CGContextScaleCTM(context, scaleRatio, -scaleRatio);
        CGContextTranslateCTM(context, 0, -height);
    }
    CGContextConcatCTM(context, transform);
    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return imageCopy;
}



+ (NSString *)MD5ByAStr:(NSString *)aSourceStr {
    const char *cStr = [aSourceStr UTF8String];
    unsigned char result[CC_MD5_DIGEST_LENGTH];
    CC_MD5(cStr, strlen(cStr), result); // This is the md5 call
    NSMutableString *ret = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH];
    for (NSInteger i=0; i<CC_MD5_DIGEST_LENGTH; i++) {
        [ret appendFormat:@"%02x", result[i]];
    }
    
    return ret;
}

//获取字符串大写首字母
+ (NSString *)transform:(NSString *)chinese
{
    NSMutableString *pinyin = [chinese mutableCopy];
    CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformMandarinLatin, NO);
    CFStringTransform((__bridge CFMutableStringRef)pinyin, NULL, kCFStringTransformStripCombiningMarks, NO);
    return [[pinyin uppercaseString] substringToIndex:1];
}

/***********************************************************************************/

+ (void)requeset_serverlist
{
//    [[MyAFSessionManager sharedMyManager] GET:ServerList_API parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        
//    
//    }];
    
//    [ProgectCatch saveserverlist:[responseDict[@"data"] objectForKey:@"cdn_server"]];
}

//图片url拼接
//+ (NSURL *)imageurlstr:(NSString *)str
//{
//    NSString *urlstr = [NSString stringWithFormat:@"%@/%@",[ProgectCatch getserverlist],str];
//    NSURL *url = [NSURL URLWithString:urlstr];
//    return url;
//}

//判断是否为整形：
+ (BOOL)isPureInt:(NSString*)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return[scan scanInt:&val] && [scan isAtEnd];
}

//判断是否为浮点形：
+ (BOOL)isPureFloat:(NSString*)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    float val;
    return[scan scanFloat:&val] && [scan isAtEnd];
}


/***********************************************************************************/

/*
 添加推送别名
 */
+ (void)addalias:(NSString *)alias
{
    //推送设置别名
//    [JPUSHService setTags:nil
//                    alias:alias
//    fetchCompletionHandle:^(int iResCode, NSSet *iTags, NSString *iAlias) {
//        
////        NSLog(@"rescode: %d, \ntags: %@, \nalias: %@\n", iResCode, iTags, iAlias);
//    }];
}

//数字
+(BOOL)validateNumber:(NSString *)number {
    NSString *emailRegex = @"[0-9.]";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:number];
}

//邮箱
+ (BOOL) validateEmail:(NSString *)email
{
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:email];
}
//手机号码验证
+ (BOOL)validateMobile:(NSString *)telnumber
{
    
    
    NSString *pattern = @"^1+[3578]+\\d{9}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", pattern];
    BOOL isMatch = [pred evaluateWithObject:telnumber];
    return isMatch;
    
    
   
//    NSString *MOBILE = @"^1[123456789]\\d{9}$";
//    NSPredicate *regexTestMobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",MOBILE];
//    if ([regexTestMobile evaluateWithObject:telnumber]) {
//        return YES;
//
//    }else {
//        return NO;
//    }
}


//车牌号验证
+ (BOOL) validateCarNo:(NSString *)carNo
{
    NSString *carRegex = @"^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[a-zA-Z_0-9]{4}[a-zA-Z_0-9_\u4e00-\u9fa5]$";
    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",carRegex];
    return [carTest evaluateWithObject:carNo];
}


//车型
+ (BOOL) validateCarType:(NSString *)CarType
{
    NSString *CarTypeRegex = @"^[\u4E00-\u9FFF]+$";
    NSPredicate *carTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",CarTypeRegex];
    return [carTest evaluateWithObject:CarType];
}



//用户名
+ (BOOL) validateUserName:(NSString *)name
{
    NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";
    NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
    BOOL B = [userNamePredicate evaluateWithObject:name];
    return B;
}


//密码
+ (BOOL) validatePassword:(NSString *)passWord
{
    NSString *passWordRegex = @"^[a-zA-Z0-9]{6,20}+$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",passWordRegex];
    return [passWordPredicate evaluateWithObject:passWord];
}


//昵称
+ (BOOL) validateNickname:(NSString *)nickname
{
    NSString *nicknameRegex = @"^[\u4e00-\u9fa5]{4,8}$";
    NSPredicate *passWordPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",nicknameRegex];
    return [passWordPredicate evaluateWithObject:nickname];
}


//身份证号
+ (BOOL) validateIdentityCard: (NSString *)identityCard
{
    BOOL flag;
    if (identityCard.length <= 0) {
        flag = NO;
        return flag;
    }
    NSString *regex2 = @"^(\\d{14}|\\d{17})(\\d|[xX])$";
    NSPredicate *identityCardPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
    return [identityCardPredicate evaluateWithObject:identityCard];
}



//判断是否有特殊符号
+ (BOOL)effectivePassword:(NSString *)str
{
    NSString *regex = @"[a-zA-Z0-9]{6,20}";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [predicate evaluateWithObject:str];
}


+ (NSString *)hidePhone:(NSString *)phone {
    if (phone.length == 11) {
        NSString *newPhone = [phone stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
        return newPhone;
    }
    return @"";
    
}

+ (NSString *)hideIdCard:(NSString *)idCard {
    if (idCard.length >= 18) {
        NSString *newIdC = [idCard stringByReplacingCharactersInRange:NSMakeRange(4, idCard.length-8) withString:@"**********"];
        return newIdC;
    }
    return @"";
}


+ (NSString *)hieUserName:(NSString *)name {
    if (name.length >= 1) {
        NSString *newName = [name stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:@"*"];
        return newName;
    }
    return @"";
}

+ (void)setNewMessage:(BOOL)isNew {
    if (isNew) {
        [[NSUserDefaults standardUserDefaults] setObject:@"yes" forKey:@"newMessage"];
    }else {
        [[NSUserDefaults standardUserDefaults] setObject:@"no" forKey:@"newMessage"];
    }

}

+ (BOOL)isHaveNewMessage {
    NSString *isNew = [[NSUserDefaults standardUserDefaults] objectForKey:@"newMessage"];
    if ([isNew isEqualToString:@"yes"]) {
        return YES;
    }
    return NO;
}


@end
